package com.swim.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * @author ljw
 * @date 2021/1/5 16:28
 */
public class DateUtil {
	
	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
    public static String formatToBJDate(String dateStr) {
        try {
            SimpleDateFormat sf = null;
            if(dateStr.length() == 16){
                sf = new SimpleDateFormat("yyyyMMdd'T'HHmmss'Z'");
            }else{
                sf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSS'Z'");
            }
            TimeZone utcZone = TimeZone.getTimeZone("UTC");
            sf.setTimeZone(utcZone);
            Date date = sf.parse(dateStr);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return sdf.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
	 * 取得系统时间
	 * @param pattern eg:yyyy-MM-dd HH:mm:ss,SSS
	 * @return
	 */
   public static String getSysTime(String pattern) {

       return formatSysTime(new SimpleDateFormat(pattern));
   }

    /**
    * 格式化系统时间
    * @param format
    * @return
    */
   private static String formatSysTime(SimpleDateFormat format) {
       String str = format.format(Calendar.getInstance().getTime());
       return str;
   }
   
   /**
    * 获取指定年月的第一天+时间 YYYY_MM_DD_HH_MM_SS
    * @param year
    * @param month
    * @return Date
    */
   public static java.sql.Date getFirstDayOfMonth1(int year, int month) {     
       Calendar cal = Calendar.getInstance();   
       //设置年份
       cal.set(Calendar.YEAR, year);
       //设置月份 
       cal.set(Calendar.MONTH, month-1); 
       //获取某月最小天数
       int firstDay = cal.getMinimum(Calendar.DATE);
       //设置日历中月份的最小天数 
       cal.set(Calendar.DAY_OF_MONTH,firstDay); 
       cal.set(Calendar.HOUR_OF_DAY, 0);
       cal.set(Calendar.MINUTE, 0);
       cal.set(Calendar.SECOND, 0);
       String str = sdf.format(cal.getTime());
       System.out.println(str);
       java.util.Date d = null; 
       try { 
        d = sdf.parse(str); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
       java.sql.Date date = new java.sql.Date(d.getTime()); 
       return date; 
   }
   
   /**
    * 获取指定年月的最后一天
    * @param year
    * @param month
    * @return Date
    */
    public static java.sql.Date getLastDayOfMonth1(int year, int month) {     
        Calendar cal = Calendar.getInstance();     
        //设置年份  
        cal.set(Calendar.YEAR, year);  
        //设置月份  
        cal.set(Calendar.MONTH, month-1); 
        //获取某月最大天数
        int lastDay = cal.getActualMaximum(Calendar.DATE);
        //设置日历中月份的最大天数  
        cal.set(Calendar.DAY_OF_MONTH, lastDay);  
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        String str = sdf.format(cal.getTime());
        System.out.println(str);
        java.util.Date d = null; 
        try { 
         d = sdf.parse(str); 
        } catch (Exception e) { 
         e.printStackTrace(); 
        } 
        java.sql.Date date = new java.sql.Date(d.getTime()); 
        return date; 
    }
    

	/**
	 * 返回当天的开始日期+时间 YYYY_MM_DD_HH_MM_SS
	 * 
	 * @return 返回该日期的字符串格式
	 */
	public static String getStartTimeOfDayByString(String pattern) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		return sdf.format(calendar.getTime());
	}
    
	/**
	 * 返回当天的开始日期+时间 YYYY_MM_DD_HH_MM_SS
	 * @return 返回该日期
	 */
	public static Date getStartTimeOfDay() {
		try {
			return sdf.parse(getStartTimeOfDayByString("yyyy-MM-dd HH:mm:ss"));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return new Date();
	}
	
	/**
	 * 返回当天的最后日期+时间
	 * @return 返回该日期的字符串格式
	 */
	public static String getEndTimeOfDayByString() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		return sdf.format(calendar.getTime());
	}
	
	/**
	 * 返回当天的最后日期+时间 YYYY_MM_DD_HH_MM_SS
	 * 
	 * @return 返回该日期
	 */
	public static Date getEndTimeOfDay() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			return sdf.parse(getEndTimeOfDayByString());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return new Date();
	}
	
	/**
	 * 
	 * 获取一个月天数
	 * @return 月天数
	 */
	public static int getDaysOfMonth(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
	} 

}
